package com.humanres.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciko.code.adapter.WorkFlowAdapter;
import com.ciko.code.base.BaseActicity;
import com.ciko.code.bean.RecodeListEntity;
import com.ciko.code.bean.User;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.Req.AgreeOverTimeFlowReq;
import com.ciko.code.http.Req.BackOverTimeFlowReq;
import com.ciko.code.http.Req.FinishOverTimeFlowReq;
import com.ciko.code.http.Req.GetOverTimeFlowNextPerson;
import com.ciko.code.http.Req.GetOverTimeFlowsBySelfEty;
import com.ciko.code.type.ResLeaveEnum;
import com.ciko.code.utils.DateUtil;
import com.ciko.code.utils.Util;
import com.ciko.code.views.PullListView;
import com.mywork.R;
import com.mywork.WorkUIKit;
import com.ui.dialog.ResDialog;
import com.ui.dialog.ResDialogListener;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by ciko on 2015/12/9.
 */
public class ResOverWorkDetailActivity
        extends
        BaseActicity
{
	
	private GetOverTimeFlowsBySelfEty getOverTimeFlowsBySelfEty;
									  
	private TextView				  tvnameOverWork;
	private TextView				  tvStateOverWork;
	private TextView				  tvcreateTimeOverWork;
	private TextView				  tvStartTimeOverWork;
	private TextView				  tvEndTimeOverWork;
	private TextView				  tvDayCountOverWork;
	private TextView				  tvResonOverWork;
	private ResLeaveEnum			  fromtype;
	private Button					  btnRejectOut;
	private Button					  btnAcceptOut;
	private Button					  btnFinish;
	private LinearLayout			  llAcceptOrReject;
	private LinearLayout			  llFinish;
	private User					  nextUser;
	private TextView				  tvaddLeave;
									  
	private PullListView			  lv_workFlow;
	private WorkFlowAdapter			  workFlowAdapter;
	private List<RecodeListEntity>	  recodeListEntityList = new ArrayList<>();
	private ResDialog				  resDialog;
	private LinearLayout			  ll;
									  
	@Override
	protected void initTopBar() {
		getOverTimeFlowsBySelfEty = (GetOverTimeFlowsBySelfEty) getIntent().getSerializableExtra("GetOverTimeFlowsBySelfEty");
		fromtype = (ResLeaveEnum) getIntent().getSerializableExtra("fromType");
		recodeListEntityList = getOverTimeFlowsBySelfEty.getWorkFlow().getRecodeList();
		setTitle("查看加班详情");
		enableNavLeftImg();
	}
	
	@Override
	protected void initLayout() {
		setContentView(R.layout.activity_res_item_over_work_detail);
	}
	
	@Override
	protected void initView() {
		ll = (LinearLayout) findViewById(R.id.ll_over);
		ll.setVisibility(View.GONE);
		tvResonOverWork = (TextView) findViewById(R.id.tvResonOverWork);
		tvDayCountOverWork = (TextView) findViewById(R.id.tvDayCountOverWork);
		tvEndTimeOverWork = (TextView) findViewById(R.id.tvEndTimeOverWork);
		tvStartTimeOverWork = (TextView) findViewById(R.id.tvStartTimeOverWork);
		tvcreateTimeOverWork = (TextView) findViewById(R.id.tv_createTimeOverWork);
		tvStateOverWork = (TextView) findViewById(R.id.tvStateOverWork);
		tvnameOverWork = (TextView) findViewById(R.id.tv_nameOverWork);
		tvaddLeave = (TextView) findViewById(R.id.tv_addLeave);
		
		llAcceptOrReject = (LinearLayout) findViewById(R.id.ll_acceptOrreject);
		llFinish = (LinearLayout) findViewById(R.id.ll_finish);
		btnRejectOut = (Button) findViewById(R.id.btnReject);
		btnAcceptOut = (Button) findViewById(R.id.btnAccept);
		btnFinish = (Button) findViewById(R.id.btnfinish);
		progressDialog2.show();
		if (null != fromtype && fromtype == ResLeaveEnum.Receive)
		{
			//            if ("流程审批".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName()) &&
			//                    WorkUIKit.getUser().getId() == getOverTimeFlowsBySelfEty.getNextUserId()) {
			//                llAcceptOrReject.setVisibility(View.VISIBLE);
			//            } else {
			//                llAcceptOrReject.setVisibility(View.GONE);
			//                llFinish.setVisibility(View.VISIBLE);
			//            }
//			if (("归档".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName()) || "流程结束".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName()) && nextUser == null) || "失败".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName()) || "成功".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName()) || getOverTimeFlowsBySelfEty.getNextUserId() != WorkUIKit.getUser().getId())
			if ("归档".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName()) || "失败".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName()) || "审批不通过".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName()) || getOverTimeFlowsBySelfEty.getNextUserId() != WorkUIKit.getUser().getId())

			{
				llAcceptOrReject.setVisibility(View.GONE);
				llFinish.setVisibility(View.GONE);

			}
			else
			{
				llAcceptOrReject.setVisibility(View.VISIBLE);
				llFinish.setVisibility(View.GONE);
			}

			if (("流程发起".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName()) ||"成功".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName()) || "审批中".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName())) && WorkUIKit.getUser().getUserPositionMark() == 12)
			{
				btnRejectOut.setVisibility(View.GONE);
				btnAcceptOut.setText("归档");
			}
		}
		else
		{
			llAcceptOrReject.setVisibility(View.GONE);
			llFinish.setVisibility(View.GONE);
		}
		lv_workFlow = (PullListView) findViewById(R.id.lv_workFlow);
	}
	
	@Override
	protected void initAdapter() {
		boolean isLast = "归档".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName())?true:false;
		workFlowAdapter = new WorkFlowAdapter(this, recodeListEntityList, R.layout.item_list_res_work_flow, getOverTimeFlowsBySelfEty.getWorkFlow(), isLast);
		lv_workFlow.setAdapter(workFlowAdapter);
	}
	
	@Override
	protected void initDates() {
		tvnameOverWork.setText(getOverTimeFlowsBySelfEty.getUserName());
		tvcreateTimeOverWork.setText(DateUtil.longToDate(getOverTimeFlowsBySelfEty.getWorkFlow().getCreateTime(), 4));
		tvaddLeave.setText(getOverTimeFlowsBySelfEty.getOverTime() < getOverTimeFlowsBySelfEty.getWorkFlow().getCreateTime() ? "是" : "否");
		tvStartTimeOverWork.setText(DateUtil.longToDate(getOverTimeFlowsBySelfEty.getOverTime(), 4));
		tvEndTimeOverWork.setText(DateUtil.longToDate(getOverTimeFlowsBySelfEty.getEndTime(), 4));
		tvDayCountOverWork.setText(Util.getDayAndHour((int) getOverTimeFlowsBySelfEty.getOverTimeDays()));
		tvResonOverWork.setText(getOverTimeFlowsBySelfEty.getOverReasom());
		tvStateOverWork.setText(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName());
		
		doRequestHttp(new GetOverTimeFlowNextPerson(getOverTimeFlowsBySelfEty.getId()));
	}
	
	@Override
	protected void initListener() {
		btnAcceptOut.setOnClickListener(this);
		btnRejectOut.setOnClickListener(this);
		btnFinish.setOnClickListener(this);
		
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		
		if (v == btnAcceptOut)
		{
			if (null != nextUser && getOverTimeFlowsBySelfEty.getUserId() != getOverTimeFlowsBySelfEty.getNextUserId())
			{
				resDialog = new ResDialog(this, new ResDialogListener()
				{
					@Override
					public void clickOk() {
						progressDialog.show();
						doRequestHttp(new AgreeOverTimeFlowReq(getOverTimeFlowsBySelfEty.getId(), nextUser.getId(), nextUser.getStaffName(), resDialog.getEidtText()));
						Log.e("ddd", "-------------------" + resDialog.getEidtText());
					}
				});
				resDialog.show();
			}
			else
			{
//				resDialog = new ResDialog(this, new ResDialogListener()
//				{
//					@Override
//					public void clickOk() {
//						progressDialog.show();
//						doRequestHttp(new FinishOverTimeFlowReq(getOverTimeFlowsBySelfEty.getId(), resDialog.getEidtText()));
//					}
//				});
//				resDialog.show();
				progressDialog.show();
				doRequestHttp(new FinishOverTimeFlowReq(getOverTimeFlowsBySelfEty.getId(), ""));
			}
		}
		else if (v == btnRejectOut)
		{
			resDialog = new ResDialog(this, new ResDialogListener()
			{
				@Override
				public void clickOk() {
					progressDialog.show();
					doRequestHttp(new BackOverTimeFlowReq(getOverTimeFlowsBySelfEty.getId(), resDialog.getEidtText()));
				}
			});
			resDialog.show();
		}
		else if (v == btnFinish)
		{
			doRequestHttp(new FinishOverTimeFlowReq(getOverTimeFlowsBySelfEty.getId(), ""));
		}
	}

	@Override
	public void onResponseSuccess(BaseResponse<?> response, String tag) {
		super.onResponseSuccess(response, tag);
		switch (tag) {
			case "GetOverTimeFlowNextPerson":
				nextUser = (User) response.getReturnObject();
				progressDialog2.dismiss();
				ll.setVisibility(View.VISIBLE);
				if (nextUser.getUserPositionMark() == 9999) {
					Util.showToast(this, R.string.params_less_tip);
				}
				break;
			case "AgreeOverTimeFlowReq":
				progressDialog.dismiss();
				showToast("同意加班成功");
				Intent intent = new Intent();
				setResult(1002, intent);
				finish();
				break;
			case "BackOverTimeFlowReq":
				progressDialog.dismiss();
				showToast("拒绝加班成功");
				setResult(1002);
				finish();
				break;
			case "FinishOverTimeFlowReq":
				showToast(nextUser == null?"归档成功":"同意审批成功");
				progressDialog.dismiss();
				Intent intent1 = new Intent();
				setResult(1002, intent1);
				finish();
				break;
		}
	}
	
	@Override
	public void onResponseFail(String tag, int resultCode) {
		super.onResponseFail(tag, resultCode);
		switch (tag) {
			case "GetOverTimeFlowNextPerson":
				progressDialog2.dismiss();
				showToast("加载失败，请稍后再试");
				finish();
				break;
			default:
				progressDialog.dismiss();
				showToast("审批失败，请稍后再试");
				break;
		}
	}
}
